package cn.edu.hqu.wel.shiyan1.service;

import cn.edu.hqu.wel.shiyan1.model.Movie;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MovieService {

    public List<Movie> getAllMovies() {
        return Movie.getAllMovies();
    }

    public Optional<Movie> getMovieById(Long id) {
        return Movie.getMovieById(id);
    }

    public void addMovie(Movie movie) {
        Movie.addMovie(movie);
    }

    public void deleteMovie(Long id) {
        Movie.deleteMovie(id);
    }

    public String uploadPoster(Long id, String fileName) {
        Movie movie = getMovieById(id).orElse(null);
        if (movie != null) {
            movie.setPosterUrl(fileName);
            return fileName;
        }
        return null;
    }

    public Movie updateMovie(Movie updatedMovie) {
        Optional<Movie> existingMovie = getMovieById(updatedMovie.getId());
        if (existingMovie.isPresent()) {
            Movie movie = existingMovie.get();
            movie.setTitle(updatedMovie.getTitle());
            movie.setDirector(updatedMovie.getDirector());
            movie.setDescription(updatedMovie.getDescription());
            movie.setPosterUrl(updatedMovie.getPosterUrl());
            return movie;
        } else {
            throw new IllegalArgumentException("Movie not found with ID: " + updatedMovie.getId());
        }
    }
}